package threadcoreknowledge.sync;

/**
 * 同时访问同步方法和非同步方法 （同时执行，非同步方法不受影响）
 */
public class SynchronizedYesAndNo6 implements Runnable {

    public static void main(String[] args) {
        SynchronizedYesAndNo6 instance = new SynchronizedYesAndNo6();
        Thread t1 = new Thread(instance);
        Thread t2 = new Thread(instance);
        t1.start();
        t2.start();
    }

    @Override
    public void run() {
        if (Thread.currentThread().getName().equals("Thread-0")) {
            method1();
        } else {
            method2();
        }

    }

    public synchronized void method1() {
        System.out.println("枷锁方法 " + Thread.currentThread().getName());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " 运行结束");
    }

    public void method2() {
        System.out.println("不枷锁方法 " + Thread.currentThread().getName());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " 运行结束");
    }
}
